package studio.raptor.hub.in.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Joiner;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import org.apache.http.client.HttpClient;
import studio.raptor.hub.in.ServerProxy;
import studio.raptor.hub.in.entity.SoCompleteARPReq;

/**
 * Created by liuyong8 on 2017/7/27.
 */
public class SoCompleteARPProxy extends ServerProxy {
//  [{
//    "orderItemIds" : [
//    1000000000031003,
//        1000000000031000,
//        1000000000031001,
//        1000000000031002
//      ]
//  }]

  private static final String REQ_MESSAGE = "[{\"orderItemIds\":[%s]}]";

  private SoCompleteARPProxy(Builder builder){
    super(builder.getHttpClient());
  }

  public static class Builder {
    private HttpClient httpClient;

    public SoCompleteARPProxy build() {
      return new SoCompleteARPProxy(this);
    }

    public Builder httpClient(HttpClient httpclient) {
      this.httpClient = httpclient;
      return this;
    }

    public HttpClient getHttpClient() {
      return httpClient;
    }

  }

  @Override
  protected String handleResponse(String response) {
    DocumentContext sc = JsonPath.parse(response);
    Object object = sc.read("$.resultObject.customerOrders");
    String result =
        JSON.toJSONString(object, SerializerFeature.PrettyFormat,
            SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
    if (result.startsWith("{")) {
      result = result.substring(1, result.length() - 1);
    }
    return result;
  }

  @Override
  protected String getMessage(Long... params) {
    return String.format(REQ_MESSAGE, Joiner.on(",").join(params));
  }
}
